package com.ruoyi.framework.security.filter;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: back
 * @Package: com.ruoyi.framework.security.filter
 * @ClassName: AccountExpiredFilter
 * @Author: haiyue
 * @Description: 账号有效期过滤器
 * @Date: 2025/5/19 下午 2:15:17
 * @Version: 1.0
 */
@Component
public class AccountExpiredFilter extends OncePerRequestFilter {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        LoginUser loginUser = tokenService.getLoginUser(request);
//        System.out.println("---------这里去写登录账号验证码过期校验"+loginUser);
        if(loginUser!=null && loginUser.getUserId()!=null){
            /*
            // 查询用户信息
            SysUser user = userService.selectUserById(loginUser.getUserId());

            // 检查账号是否过期
            if (userService.checkExpireTime(user)) {
                // 账号已过期，返回403状态码
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write(JSON.toJSONString(AjaxResult.error(HttpStatus.EXPIRED_DATE, "账号有效期已经过期，请进行续费")));
                return;
            }*/
        }
        // 继续过滤器链
        filterChain.doFilter(request, response);
    }
}
